package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

import play.data.validation.*;

public class Neighborhoods extends Controller
{
	public static void index()
	{
		List<Neighborhood> neighborhoods = Neighborhood.findAll();
	
		render(neighborhoods);
	}

	public static void show(Long id)
	{
		Neighborhood neighborhood = Neighborhood.findById(id);
	
		render(neighborhood);		
    }
	
	public static void create()
	{
		render();
	}
	
	public static void save(@Valid Neighborhood neighborhood)
	{
		if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			create();
		}
	
		neighborhood.save();
		
		show(neighborhood.id);
	}
	
	public static void update(Long id)
	{
		Neighborhood neighborhood = Neighborhood.findById(id);
		
		render(neighborhood);
	}
	
	public static void doUpdate(@Valid Neighborhood neighborhood)
	{
		if(validation.hasErrors())
		{
			params.flash();
			validation.keep();
			update(neighborhood.id);
		}
	
		neighborhood.save();
		
		show(neighborhood.id);
	}
	
	public static void delete(Long id)
	{
		Neighborhood neighborhood = Neighborhood.findById(id);
		
		neighborhood.delete();
		
		index();
	}
}